#!/usr/bin/python
# -*- coding: UTF-8 -*-

import sys, os
from loguru import logger

from PyQt5.QtWidgets import QApplication, QWidget

from qt_material import apply_stylesheet

sys.path.append(os.getcwd())
from views.widgets import Ui_register
from models import experiment_record

class RegisterForm(QWidget, Ui_register.Ui_RegisterForm):
    def __init__(self, parent = None):
        super(RegisterForm, self).__init__(parent)
        self.setupUi(self)

        self.parentMainWindow = None
        self.CancelButton.clicked.connect(self.cancelRegister)
        self.NextStepButton.clicked.connect(self.nextStep)

        ## TODO 生成一个唯一的ID
        self.experimentID
        ## TODO 设置为当前时间
        self.experimentDate

        self.experimentRecord = None
        logger.debug("create an instance of RegisterForm successfully")     

    def setMainWindow(self, MainWindow):
        self.parentMainWindow = MainWindow
    
    # experiment_record.PAIExperimentRecord
    def setExperimentRecord(self, experimentRecord):
        self.experimentRecord = experimentRecord

    def nextStep(self):
        # set value for experiment record
        if self.experimentRecord:
            self.experimentRecord.setExperimentID(self.experimentID.text())
            self.experimentRecord.setExperimentPerson(self.experimentPerson.text())
            self.experimentRecord.setExperimentDate(self.experimentDate.text())
            self.experimentRecord.setExperimentObject(self.experimentObject.text())
            self.experimentRecord.setExperimentComments(self.experimentComments.text())

        if self.parentMainWindow is not None:
            self.parentMainWindow.updateExperimentRecord(self.experimentRecord)
            self.parentMainWindow.registerNextStep()
            logger.debug("regiter a new experiment successfully.")
        else:
            pass

    def cancelRegister(self):
        # TODO: add other logic
        if self.parentMainWindow is not None:
            self.parentMainWindow.returnHome()
        else:
            pass


if __name__ == "__main__":
    app = QApplication(sys.argv)
    PAWin = RegisterForm()
    apply_stylesheet(app, theme = "dark_teal.xml")
    PAWin.show()
    sys.exit(app.exec_())